import http from "@/utils/http.js"

import advertUtil from '@/utils/curd/advertUtil.js'

export const articleMixin = {
	data() {
		return {
			//小程序信息
			appletInfo: null,
			//集数
			numberVal: null,
			//解锁资源ID
			unlockResourceId: null,
			showUnLock: false
		}
	},
	onLoad() {
		this.getAppletInfo()
		// #ifdef MP
		//加载广告数据
		advertUtil.advertInfo(this, ()=>{
			//加载插屏
			advertUtil.setInterstitialAd(this); 
			//加载激励视频
			advertUtil.loadRewardedVideoAd(this,
			()=>{
				console.log('播放完毕，下发奖励')
				this.saveCreatorIncome()
				this.savePlayRecord()
			},
			()=>{
				console.log('未播放完毕，无奖励')
			});
		});
		// #endif
	},
	onHide(){
		advertUtil.clearAdInterval();
	},
	methods: {
		getAppletInfo(){
			var params = {
				url: `/baseInfo/getAppletInfo`,
				method: 'GET',
				data: {},
				callBack: (res) => {
					if(res.code == 200){
						this.appletInfo = res.data
					}else{
						uni.showToast({
							title: '获取信息失败,请重新进入'
						})
					}
				}
			};
			http.request(params);
		},
		seeVideo(resourceId){
			uni.navigateTo({
				url: `/pages/filmVideo/filmVideo?resourceId=${resourceId}`,
			})
		},
		//定位上次播放和搜索集数
		position(e){
			var params = {
				url: `/film/movie/u/position`,
				method: 'GET',
				data: {
					filmId: this.id
				},
				callBack: (res) => {
					if(res.code == 200){
						this.seeVideo(res.data.id)
					}
				}
			};
			http.request(params);
		},
		//解锁资源
		unlockResource(val){
			this.unlockResourceId = val
			if(!val){
				uni.showToast({
					title: '解锁失败',
					icon: 'none'
				})
				return
			}
			
			//此处需要展示弹窗，需用户点击确认后看广告或者免广告直接解锁
			this.showUnLock = true
			
		},
		goUnlock(){
			
			//验证是否需要显示广告-主要第一次审核时需要关闭广告
			//1 是显示广告
			if(1 == this.appletInfo.showAd){
				//看广告方法,可以直接调用readMore
				advertUtil.lookRewardedVideoAd()
			}else{
				//无广告直接解锁
				this.savePlayRecord(this.unlockResourceId)
			}
		},
		//添加创作者收益
		saveCreatorIncome(){
			if(this.appletInfo){
				let sysInfo = uni.getSystemInfoSync();
				let platform = sysInfo.platform;
				//软件版本号  sysInfo.version
				//客户端平台，值域为：ios、android、mac（3.1.10+）、windows（3.1.10+）、linux（3.1.10+）
				//判断用户的手机类型
				let closeProfit = this.appletInfo.closeProfit
				if(closeProfit == 1 && platform == 'ios'){
					console.log("关闭IOS");
					return;
				}else if(closeProfit == 2 && platform == 'android'){
					console.log("关闭安卓");
					return;
				}else if(closeProfit == 3){
					console.log("关闭全部");
					return;
				}
			}
			var params = {
				url: `/pbalance/u/save`,
				method: 'POST',
				data: {
					wid: this.unlockResourceId,
				},
				callBack: (res) => {}
			};
			http.request(params);
		},
		//保存播放记录
		savePlayRecord(){
			var params = {
				url: `/film/playRecord/u/addOrUpdate/${this.unlockResourceId}`,
				method: 'POST',
				data: {},
				callBack: (res) => {
					if(res.code == 200){
						this.seeVideo(this.unlockResourceId)
						this.ids.push(this.unlockResourceId)
						this.filerFilm()
						this.showUnLock = false
					}
				}
			};
			http.request(params);
		}
		
	}
}